package com.shanzmoo.pay.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author dingmeng
 * @title: HttpClientUtil
 * @projectName jyf-mall-server
 * @description: TODO
 * @date 2021/4/418:32
 */
public class HttpClientUtil {
    public HttpClientUtil() {
    }

    public static String post(String url, String jsonStr, int timeout, String ContentType, String charset) throws Exception {
        String res = "";
        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", ContentType);
            StringEntity enty = new StringEntity(jsonStr.toString(), "utf-8");
            httpPost.setEntity(enty);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000).setSocketTimeout(timeout).build();
            httpPost.setConfig(requestConfig);
            CloseableHttpResponse response = httpclient.execute(httpPost);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    res = EntityUtils.toString(entity, "utf-8");
                }

                EntityUtils.consume(entity);
            } catch (Exception var27) {
                throw var27;
            } finally {
                response.close();
            }
        } catch (Exception var29) {
            throw var29;
        } finally {
            try {
                httpclient.close();
            } catch (Exception var26) {
                throw var26;
            }
        }

        return res;
    }
}
